package com.gexiaobao.pigeon.ui.breeding_pigeon

import android.content.Intent
import android.os.Bundle
import android.view.inputmethod.EditorInfo
import androidx.core.view.isVisible
import androidx.core.widget.addTextChangedListener
import androidx.recyclerview.widget.LinearLayoutManager
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.base.BaseActivity
import com.gexiaobao.pigeon.app.eventViewModel
import com.gexiaobao.pigeon.app.ext.hideSoftKeyboard
import com.gexiaobao.pigeon.app.ext.init
import com.gexiaobao.pigeon.app.ext.initFooter
import com.gexiaobao.pigeon.app.ext.loadListData
import com.gexiaobao.pigeon.app.ext.loadServiceInit
import com.gexiaobao.pigeon.app.ext.showLoading
import com.gexiaobao.pigeon.app.util.RxToast
import com.gexiaobao.pigeon.app.util.StatusBarUtil
import com.gexiaobao.pigeon.app.util.Util
import com.gexiaobao.pigeon.app.weight.recycleview.SpaceItemDecoration
import com.gexiaobao.pigeon.databinding.ActivityBreedPigeonSelectionBinding
import com.gexiaobao.pigeon.ui.adapter.AdapterBloodPigeonSelection
import com.gexiaobao.pigeon.viewmodel.BreedingPigeonViewModel
import com.kingja.loadsir.core.LoadService
import me.hgj.jetpackmvvm.ext.util.setOnclickNoRepeat
import me.hgj.jetpackmvvm.util.finish
import me.hgj.jetpackmvvm.util.startActivity

/**
 * @Author      : hxw
 * @Date        : 2024/9/3 15:29
 * @Describe    : 种鸽选择
 */
class ActivityBreedPigeonSelection : BaseActivity<BreedingPigeonViewModel, ActivityBreedPigeonSelectionBinding>() {

    private var mSex = ""
    private var isAmend = false//true-可编辑性别，false-不可编辑性别
    private lateinit var loadSir: LoadService<Any>
    private val mAdapter: AdapterBloodPigeonSelection by lazy { AdapterBloodPigeonSelection(arrayListOf()) }

    override fun initView(savedInstanceState: Bundle?) {
        StatusBarUtil.setTranslucentForImageView(this, 0, null)
        val title = intent.getStringExtra("title")
        mDatabind.includeBar.tvTitle.text = title
        isAmend = intent.getBooleanExtra("isAmend", false)
        mSex = if (title == "雌鸽选择") {
            getString(R.string.hint_female)
        } else {
            getString(R.string.hint_male)
        }
        mDatabind.includeSearch.editQuery.hint = "请输入足环号或种鸽名称"

        mDatabind.includeSearch.editQuery.setOnEditorActionListener { _, i, _ ->
            when (i) {
                EditorInfo.IME_ACTION_SEARCH -> {
                    onRefresh(isRefresh = true)
                    hideSoftKeyboard(this)
                }

                else -> {}
            }
            false
        }

        mDatabind.includeSearch.editQuery.addTextChangedListener {
            mDatabind.includeSearch.ivClose.isVisible = it.toString().isNotEmpty()
        }

        loadSir = loadServiceInit(mDatabind.includeList.swipeRefreshLayout) {
            onRefresh(isRefresh = true)
        }

        onRefresh(isRefresh = true)

        mDatabind.includeList.swipeRecyclerView.init(LinearLayoutManager(this), mAdapter).let {
            it.addItemDecoration(SpaceItemDecoration(0, Util.dp2px(this, 12f)))
            it.initFooter {
                onRefresh(isRefresh = false)
            }
        }

        mDatabind.includeList.swipeRefreshLayout.init {
            onRefresh(isRefresh = true)
        }

        mAdapter.addChildClickViewIds(R.id.btnSelected)
        mAdapter.setOnItemChildClickListener { _, _, position ->
            intentFinishTo(mAdapter.data[position].id)
        }

        mAdapter.setOnItemClickListener { _, _, position ->
            intentFinishTo(mAdapter.data[position].id)
        }

        eventViewModel.toRefreshData.observeInActivity(this) {
            onRefresh(isRefresh = true)
        }
    }

    private fun intentFinishTo(id: Int) {
        val intent = Intent()
        intent.putExtra("id", id)
        finish(intent)
    }

    private fun onRefresh(isRefresh: Boolean) {
        val ringOrName = mDatabind.includeSearch.editQuery.text.toString().trim()
        if (isRefresh) {
            loadSir.showLoading()
        }
        showLoading(getString(R.string.loading))
        mViewModel.getBreedingPigeonList(isRefresh, "", "", mSex, "", ringOrName, "", "", "")
    }

    override fun createObserver() {
        super.createObserver()
        /** 获取种鸽列表 */
        mViewModel.getPigeonResult.observe(this) {
            loadListData(it, mAdapter, loadSir, mDatabind.includeList.swipeRecyclerView, mDatabind.includeList.swipeRefreshLayout)
        }
    }

    override fun onBindViewClick() {
        super.onBindViewClick()
        setOnclickNoRepeat(
            mDatabind.includeBar.ivBack,
            mDatabind.btnAddBloodPigeon,
            mDatabind.includeSearch.ivClose,
            mDatabind.includeSearch.tvSearch
        ) {
            when (it) {
                mDatabind.includeSearch.tvSearch -> {
                    if (mDatabind.includeSearch.editQuery.text.toString().trim().isEmpty()) {
                        RxToast.warning("请输入搜索内容")
                        return@setOnclickNoRepeat
                    }
                    onRefresh(isRefresh = true)
                }

                mDatabind.includeSearch.ivClose -> {
                    mDatabind.includeSearch.editQuery.setText("")
                    onRefresh(isRefresh = true)
                }

                mDatabind.btnAddBloodPigeon -> startActivity<ActivityAddBloodPigeon>("type" to 1, "isAmend" to false)
                mDatabind.includeBar.ivBack -> finish()
            }
        }
    }
}